import Math.LinearEquationSolver
import System.IO.Unsafe
import Data.Maybe (isJust, fromJust, catMaybes)
import Utils (tok)
import Debug.Trace (trace)
import System.Process

type Coord = (Integer, Integer, Integer)
type Entry = (Coord, Coord)

parseLine :: String -> Entry
parseLine l = 
    ((x, y, z), (vx, vy, xz))
    where
        toks         = map read . tok ", @" $ l
        (x, y, z)    = (toks !! 0, toks !! 1, toks !! 2)
        (vx, vy, vz) = (toks !! 3, toks !! 4, toks !! 5)

intersect2d :: Entry -> Entry -> Maybe (Rational, Rational)
intersect2d ((x1, y1, _), (vx1, vy1, _)) ((x1, y1, _), (vx1, vy1, _)) = 
    if (isJust sol) && t1 > 0 && t2 > 0 then
        Just (x1', y1')
    else Nothing
    where
        a = [[vx1, -(vx2)], [vy1, -(vy2)]]
        b = [x2 - x1, y2 - y1]
        sol = unsafePerformIO $ solveRationalLinerqEqs Z3